

package com.platform.api.bzproject.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.api.bzproject.dao.UserMapper;
import com.platform.api.bzproject.model.User;
import com.platform.api.bzproject.param.UserRegisterParam;
import com.platform.common.exception.PlatformBindException;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private UserMapper userMapper;

    public User getUserByUserId(String userId) {
        return userMapper.getUserByUserId(userId);
    }


    public Boolean insertUser(UserRegisterParam uParam) {
        User mail = userMapper.getUserByUserMail(uParam.getUserMail());
        if (mail != null) {
            throw new PlatformBindException("账号已存在");
        }
        Date now = new Date();
        User user = new User();
        user.setUserId(IdUtil.simpleUUID());
        //user.setUserId(UniqueIdGenerate.getUniqueID().toString());
        user.setModifyTime(now);
        user.setUserRegtime(now);
        user.setStatus(1);
        user.setNickName(uParam.getUserMail());
        user.setUserMail(uParam.getUserMail());
        user.setLoginPassword(uParam.getPassword());
        return userMapper.insert(user) == 1;
    }

    public Boolean insertUserPhone(UserRegisterParam uParam) {
        User mail = userMapper.getUserByUserMobile(uParam.getUserPhone());
        if (mail != null) {
            throw new PlatformBindException("账号已存在");
        }
        Date now = new Date();
        User user = new User();
        user.setUserId(IdUtil.simpleUUID());
        //user.setUserId(UniqueIdGenerate.getUniqueID().toString());
        user.setModifyTime(now);
        user.setUserRegtime(now);
        user.setStatus(1);
        user.setNickName(uParam.getUserMail());
        user.setUserMail(uParam.getUserMail());
        user.setLoginPassword(uParam.getPassword());
        return userMapper.insert(user) == 1;
    }


    public List<User> getUserByList(@Param("userRegTime") String userRegTime) {
        return userMapper.getUserByList(userRegTime);
    }


}
